package it.uniroma3.dia.digraph.algorithm.utility.kouterplanar.outerplanar;

import java.util.Map;

import org.graphdrawing.graphml.xmlns.NodeType;

public class OuterplanarNode {
	
	private NodeType node;
	private int consideredDegree;
	private Map<OuterplanarNode, Double> parent2lambda;
	
	public OuterplanarNode(){
	    this (null, 0);
	}
	
	public OuterplanarNode(NodeType node) {
	    this(node, node.getParseIndegree().intValue()+ node.getParseOutdegree().intValue());
	}
	
	public OuterplanarNode(NodeType node, int degree){
		this.node = node;
		
		this.consideredDegree = degree;//node.getParseIndegree().intValue() + node.getParseOutdegree().intValue();
		this.parent2lambda = null;
	}
	
	/**
	 * Decrease the considered degree for this node.
	 * If the considered degree for this node is 2, it will not be changed.
	 */
	public void decreaseConsideredDegree() {
		if (this.consideredDegree > 2)
			this.consideredDegree--;
		
	}
	
	public void setParent2Lambda(Map<OuterplanarNode, Double> par2lambda) {
	    this.parent2lambda = par2lambda;
	}
	
	public double getX() {
	    double x = 0;
	    if (this.parent2lambda==null)
		return this.node.getX();
	    
	    for (OuterplanarNode parent : this.parent2lambda.keySet())
		x += this.parent2lambda.get(parent)* parent.getX();

	    
	    
	    return x;	    
	}
	
	public double getY() {
	    double y = 0;
	    if (this.parent2lambda == null)
		return this.node.getY();
	    
	    for (OuterplanarNode parent : this.parent2lambda.keySet())
		y += this.parent2lambda.get(parent)*parent.getY();
	    
	    return y;
	}
	
	public void updateCoords() {
	    System.out.print(this+" changed from ("+this.node.getX()+","+this.node.getY()+") to");
	    this.node.setX(this.getX());
	   this.node.setY(this.getY());
	   System.out.println("("+this.node.getX()+","+this.node.getY()+")");
	   
	    
	}
	
	
	
	
	
	
	
	
	
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		OuterplanarNode other = (OuterplanarNode) obj;
		
		if (node == null) {
			if (other.node != null)
				return false;
		} 
			
		return this.node.equals(other.node);
	}
	/**
	 * @return the consideredDegree
	 */
	public int getConsideredDegree() {
		return consideredDegree;
	}
	public String getId() {		
		return this.node.getId();
	}

	/**
	 * @return the node
	 */
	public NodeType getNode() {
		return node;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		
		result = prime * result + ((node == null) ? 0 : node.hashCode());
		return result;
	}

	/**
	 * @param consideredDegree the consideredDegree to set
	 */
	public void setConsideredDegree(int consideredDegree) {
		this.consideredDegree = consideredDegree;
	}

	/**
	 * @param node the node to set
	 */
	public void setNode(NodeType node) {
		this.node = node;
	}
	
	public String toString(){
		return this.node.toString();
	}
}
